﻿# Weapons.py: the animation module
# Written by Victor Luna Freire - victorclf@live.com
# Copyright (c) Victor Luna Freire. All rights reserved.
# Sunday, June 25, 2009 22:45:00 PM
# Python 2.6.2

import pygame
import Animation
import Gamescript

class Weapon:
    def __init__(self, template):
        DEFAULT_PATH = "weapon"
        Y_OFFSET = 80
        self.template = template
                        
        if template == "shotgun":
            self.damage = 50
            self.ammo = 100
            self.max_ammo = 100
            self.shoot_frames = 4 #shooting animation frames
            self.fire_frames = 2  #animation frames for the fire on the barrel
            # time in milliseconds
            self.shoot_time = [140] * self.shoot_frames
            self.shoot_time[0] = 270
            self.shoot_time[3] = 160
            self.shoot_time[3] = 110
            
            self.fire_time = [75] * 2
            self.weapon = Animation.Animation((DEFAULT_PATH, self.template, 'w'), (0, Y_OFFSET), self.shoot_frames, self.shoot_time, 'full-pass')
            self.barrelfire = Animation.Animation((DEFAULT_PATH, self.template, 'f'), (0, Y_OFFSET), self.fire_frames, self.fire_time, 'half-pass')
        else:
            print "Warning: weapon template not found -", template
            
            
    def fire(self, aimed_enemies):
        if not self.weapon.is_playing() and self.ammo:
            self.weapon.play()
            self.barrelfire.play() #barrel fire
            self.ammo -= 1
            self.check_hits(aimed_enemies)
            return True
        return False
    
    def check_hits(self, aimed_enemies):
        if not aimed_enemies: return
        victim = min(aimed_enemies)[1]
        victim.take_hit(self.damage)
    
    def recharge(self):
        self.ammo = self.max_ammo
    
    def draw(self, screen):
        self.weapon.draw(screen)
        if self.barrelfire.is_playing(): self.barrelfire.draw(screen)
        return True
          
                    
class Weapons:
    def __init__(self):
        self.weapons = {"shotgun": Weapon("shotgun")}
        self.current_weapon = self.weapons["shotgun"]
        
    def recharge(self):
        self.current_weapon.recharge()
        
    def fire(self, aimed_enemies):
        result = self.current_weapon.fire(aimed_enemies)
        if result: Gamescript.SoundDirector.sound_event('shotgun')
        return result
    
    
    def draw(self, screen):
        return self.current_weapon.draw(screen)
